package com.acme.domain.client;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import com.acme.exceptions.ClientExistsException;

public class Bank implements Serializable {

    private static final long serialVersionUID = -7132395463484781019L;
    private Set<Client> clients = new HashSet<>();

    public Client addClient(final Client client) throws ClientExistsException {
	if (getClientByName(client.getName()) != null) {
	    throw new ClientExistsException("Client with name: "
		    + client.getName() + " has already in the bank", client);
	}
	clients.add(client);
	return client;
    }

    public Client getClientByName(final String name) {
	for (Client client : clients) {
	    if (client.getName().equals(name)) {
		return client;
	    }
	}
	return null;
    }

    public List<Client> getClientsList() {
	List<Client> list = new LinkedList<>(clients);
	return Collections.unmodifiableList(list);
    }

    @Override
    public String toString() {

	StringBuffer clientsString = new StringBuffer("The bank has: \n");
	for (Client client : clients) {
	    clientsString.append(client);
	}
	return clientsString.toString();
    }

}
